package cn.janyd.jmall.system.service.admin;

import cn.janyd.common.core.vo.CommonResult;
import cn.janyd.common.core.vo.PageQuery;
import cn.janyd.common.core.vo.PageResult;
import cn.janyd.jmall.system.convert.AdminConverter;
import cn.janyd.jmall.system.dal.db.dataobject.AdminDO;
import cn.janyd.jmall.system.dal.db.mapper.AdminMapper;
import cn.janyd.jmall.system.manager.admin.AdminManager;
import cn.janyd.jmall.system.rpc.admin.AdminRpcService;
import cn.janyd.jmall.system.rpc.admin.dto.*;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.AllArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.stereotype.Service;

import static cn.janyd.common.core.vo.CommonResult.success;

/**
 * @author Janyd
 * @since 2020-08-15 1:17
 */
@AllArgsConstructor
@Service
@DubboService(version = "${jmall.dubbo.versions.SystemRpcService}")
public class AdminServiceImpl implements AdminRpcService {

    private final AdminMapper adminMapper;

    private final AdminManager adminManager;

    /**
     * 获取管理员信息
     *
     * @param adminId 管理员ID
     * @return 管理员信息
     */
    @Override
    public CommonResult<AdminRespDTO> getAdmin(Integer adminId) {
        AdminDO adminDO = adminMapper.selectById(adminId);
        return success(AdminConverter.INSTANCE.convert(adminDO));
    }

    /**
     * 分页获取管理列表
     *
     * @param pageQuery 分页参数
     * @return 分页管理员数据
     */
    @Override
    public CommonResult<PageResult<AdminRespDTO>> pageAdmin(PageQuery<AdminPageDTO> pageQuery) {

        AdminPageDTO adminPage = pageQuery.getParam();

        Page<AdminDO> page = new Page<>(pageQuery.getPage(), pageQuery.getSize());
        page = adminMapper.selectPage(page, Wrappers.<AdminDO>lambdaQuery()
                .like(StringUtils.isNotBlank(adminPage.getSearch()), AdminDO::getName, adminPage.getSearch())
                .eq(AdminDO::getDepartmentId, adminPage.getDepartmentId()));
        return success(AdminConverter.INSTANCE.convert(page));
    }

    /**
     * 创建管理员
     *
     * @param adminCreateReq 管理员信息
     * @return 管理员信息
     */
    @Override
    public CommonResult<AdminRespDTO> createAdmin(AdminCreateReqDTO adminCreateReq) {
        AdminDO adminDO = adminManager.createAdmin(AdminConverter.INSTANCE.convert(adminCreateReq));
        return success(AdminConverter.INSTANCE.convert(adminDO));
    }

    /**
     * 更新管理员
     *
     * @param adminUpdateReq 管理员信息
     * @return 是否成功
     */
    @Override
    public CommonResult<Boolean> updateAdmin(AdminUpdateReqDTO adminUpdateReq) {
        adminManager.updateAdmin(AdminConverter.INSTANCE.convert(adminUpdateReq));
        return success(true);
    }

    /**
     * 验证密码
     *
     * @param adminVerifyPassword 验证信息
     * @return 管理员信息
     */
    @Override
    public CommonResult<AdminRespDTO> verifyPassword(AdminVerifyPasswordDTO adminVerifyPassword) {
        AdminDO adminDO = adminManager.verifyPassword(adminVerifyPassword.getUsername(), adminVerifyPassword.getPassword(), adminVerifyPassword.getIp());
        return success(AdminConverter.INSTANCE.convert(adminDO));
    }
}
